package com.chuangjiangx.member.manager.web.web.coupon.controller;


import com.chuangjiangx.commons.response.CamelResponse;
import com.chuangjiangx.commons.response.ResponseUtils;
import com.chuangjiangx.member.coupon.ddd.dal.dto.MbrActivityPullNewEffect;
import com.chuangjiangx.member.coupon.ddd.dal.dto.MbrActivityRetention;
import com.chuangjiangx.member.coupon.ddd.dal.dto.MbrCouponMarketingInfo;
import com.chuangjiangx.member.coupon.ddd.dal.dto.MbrReceiveByDay;
import com.chuangjiangx.member.coupon.ddd.query.MbrCouponMarketingQuery;
import com.chuangjiangx.member.manager.basic.web.controller.BaseController;
import com.chuangjiangx.member.manager.basic.web.interceptor.Login;
import com.chuangjiangx.member.manager.common.Constants;
import com.chuangjiangx.member.manager.common.ThreadLocalUser;
import com.chuangjiangx.member.manager.web.web.coupon.response.CouponMarketingResponse;
import com.sun.jmx.snmp.ThreadContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 卡券营销接口
 * @author cj-yzh
 * @date 2018/8/8 下午5：24
 */
@Api(value = "卡券营销接口" , tags = "卡劵")
@RestController

@RequestMapping("/api/mbr/coupon")
public class MbrCouponMarketingController extends BaseController {

    @Autowired
    private MbrCouponMarketingQuery couponMarketingQuery;

    @ApiOperation(value = "营销分析" , httpMethod = "GET")
    @RequestMapping("/marketing")
    @Login
    public CamelResponse<CouponMarketingResponse> getData (String name){
        //获得登陆对象
        ThreadLocalUser token = (ThreadLocalUser) ThreadContext.get(Constants.CURRENT_LOGIN_USER_KEY);

        Long merchantId = token.getMerchantId();

        CouponMarketingResponse response = new CouponMarketingResponse();
        MbrCouponMarketingInfo gerneralInfo = couponMarketingQuery.getGerneralInfo(merchantId, name);
        if(gerneralInfo == null){
            return null;
        }
        List<MbrReceiveByDay> mbrReceiveByDays = couponMarketingQuery.receiveCouponByDay(merchantId, name);
        //按天领取卡劵得时间和数量
        List<String> time = new ArrayList<>();
        List<Integer> newMembers = new ArrayList<>();
        for (MbrReceiveByDay mbrReceiveByDay : mbrReceiveByDays) {
            String timeStr = mbrReceiveByDay.getTimeStr();
            String year = timeStr.substring(0, 4);
            String month = timeStr.substring(4, 6);
            String day = timeStr.substring(6);
            String result = year+"-"+month+"-"+day;
            time.add(result);
            newMembers.add((int)mbrReceiveByDay.getNewMembers());
        }
        response.setTime(time);
        response.setNewNumber(newMembers);
        //活动拉新效果
        MbrActivityPullNewEffect mbrActivityPullNewEffect = couponMarketingQuery.activityPullNewEffect(merchantId, name);
        response.setCouponMarketingInfo(gerneralInfo);
        response.setActivityPullNewEffect(mbrActivityPullNewEffect);
        //活动留存效果
        MbrActivityRetention retention = couponMarketingQuery.activityRetention(merchantId,name);
        response.setActivityRetention(retention);
        return (CamelResponse)ResponseUtils.successCamel(response);
}

}
